package com.xja.web.domain.controllers;

import com.github.wxpay.sdk.WXPay;
import com.xja.web.domain.commons.DateUtil;
import com.xja.web.domain.commons.MyConfig;
import com.xja.web.domain.model.JsonBean;
import com.xja.web.domain.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("payapi")
    public JsonBean payAPI(){
        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "微信支付测试");
        String time = DateUtil.getSdfTimes();
        data.put("out_trade_no", "code"+time+"uuid");
        data.put("fee_type", "CNY");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://adu867g.hn3.mofasuidao.cn/wechat/PayController/notify");
        data.put("trade_type", "JSAPI");  // 此处指定为扫码支付
        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  new JsonBean(-1,"",null);
    }

//    @PostMapping("getQrCode")
//    public JsonBean getQrCode(){
//        MyConfig config = new MyConfig();
//        WXPay wxpay = new WXPay(config);
//        Map<String, String> data = new HashMap<String, String>();
//        data.put("body", "微信支付测试");
//        String time = DateUtil.getSdfTimes();
//        data.put("out_trade_no", "code"+time+"uuid");
//        data.put("fee_type", "CNY");
//        data.put("total_fee", "1");
//        data.put("spbill_create_ip", "123.12.12.123");
//        data.put("notify_url", "http://adu867g.hn3.mofasuidao.cn/wechat/PayController/notify");
//        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
//        try {
//            Map<String, String> resp = wxpay.unifiedOrder(data);
//            if (resp.get("return_code").equals("SUCCESS")&&resp.get("result_code").equals("SUCCESS")){
//                HashMap<String, Object> Map = new HashMap<>();
//                Map.put("code",resp.get("code_url"));
//                Map.put("orderid",data.get("out_trade_no"));
//                orderService.insertOrder(data.get("out_trade_no"));
//                return  new JsonBean(0,"",Map);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return  new JsonBean(-1,"",null);
//    }
    @PostMapping("gueryorder")
    public JsonBean gueryorder(String orderid){
        return  orderService.selectOrder(orderid);
    }
}
